package com.lnj.messages.servise;

import com.lnj.messages.quarzJob.RecommenderJob;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class RecommenderService {
    @Autowired
    private final Scheduler quartzScheduler;


    public void recommendItems(Long userId) {
        try {
            JobDetail job = JobBuilder.newJob(RecommenderJob.class)
                    .withIdentity("recommend_" + userId)
                    .usingJobData("userId", userId)
                    .build();

            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("recommend_trigger_" + userId)
                    .withSchedule(CronScheduleBuilder.cronSchedule("0 0 2 * * ?")) // 每天凌晨2点执行
                    .build();
            quartzScheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            log.error("推荐商品定时任务创建失败", e);
        }
    }
}
